package com.bilibili.dubbo.api;

import com.bilibili.dubbo.api.mongo.FriendApi;
import com.bilibili.dubbo.api.mongo.UserLikeApi;
import com.bilibili.model.mongo.UserLike;
import com.bilibili.model.vo.UserInfoVoLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author HuangRongFu
 * 2021/11/25 星期四5:45 下午
 */

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private FriendApi friendApi;

    /**
     * 添加喜欢或不喜欢数据
     *
     * @param userLike
     * @return
     */
    @Override
    public Boolean save(UserLike userLike) {

        long timeMillis = System.currentTimeMillis();

        //先保存到数据表中UserLike
        userLike.setUpdated(timeMillis);

        //判断数据是否存在
        Query query = new Query(Criteria.where("userId").is(userLike.getUserId())
                .and("likeUserId").is(userLike.getLikeUserId()));

        //存在更新
        if (mongoTemplate.exists(query, UserLike.class)) {

            Update update = new Update();

            update.set("updated", timeMillis);
            update.set("isLike", true);

            mongoTemplate.updateFirst(query, update, UserLike.class);
        } else {

            //不存在新增
            userLike.setCreated(timeMillis);
            mongoTemplate.insert(userLike);
        }

        //判断两人是否是双向喜欢
        if (userLike.getIsLike()) {

            //构建查询条件
            Query isFriendQuery = new Query(Criteria.where("likeUserId")
                    .is(userLike.getUserId()).and("isLike").is(true)
                    .and("userId").is(userLike.getLikeUserId()));

            //是 两人添加成为好友
            if (mongoTemplate.exists(isFriendQuery, UserLike.class)) {

                friendApi.addContact(userLike.getUserId(), userLike.getLikeUserId());
                return true;
            }
        }

        return false;
    }

    /**
     * 查询相互喜欢用户id
     * @param userId
     * @return
     */
    @Override
    public List<Long> findLikeUserIds(Long userId) {

        Query query = new Query(Criteria.where("userId").is(userId).and("isLike").is(true));

        //我喜欢的用户
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //喜欢用户的id
        List<Long> userLikeIds = userLikeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        List<Long> ids = new ArrayList<>();

        for (Long userLikeId : userLikeIds) {

            Query likeQuery = new Query(Criteria.where("userId")
                    .is(userLikeId).and("likeUserId").is(userId).and("isLike").is(true));

            if (mongoTemplate.exists(likeQuery, UserLike.class)) {

                ids.add(userLikeId);
            }
        }

        return ids;
    }

    /**
     * 喜欢用户列表
     * @param userId
     * @return
     */
    @Override
    public List<Long> findLikeUser(Long userId) {

        Query query = new Query(Criteria.where("userId").is(userId).and("isLike").is(true));

        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        List<Long> ids = userLikeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        return ids;
    }

    /**
     * 粉丝列表
     * @param userId
     * @return
     */
    @Override
    public List<Long> findFans(Long userId) {

        Query query = new Query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));

        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        List<Long> ids = userLikeList.stream().map(UserLike::getUserId).collect(Collectors.toList());

        return ids;
    }

    @Override
    public List<UserInfoVoLike> findAlreadyLove(List<UserInfoVoLike> voList, Long userId) {

        for (UserInfoVoLike voLike : voList) {

            Query query = new Query(Criteria.where("userId").is(userId)
                    .and("likeUserId").is(voLike.getId()));

            UserLike one = mongoTemplate.findOne(query, UserLike.class);

            voLike.setAlreadyLove(one.getIsLike());
        }

        return voList;
    }

}
